package com.thirtydays.frame.ui.round

import android.annotation.TargetApi
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.RippleDrawable
import android.graphics.drawable.StateListDrawable
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.appcompat.widget.AppCompatTextView
import com.blankj.utilcode.util.LogUtils
import com.thirtydays.frame.R

/**
 * @author 小垚
 * @创建时间： 2021/4/21
 * @描述：统一圆角处理View
 **/
class RoundViewDelegate(view: View, context: Context, attrs: AttributeSet?) {

    private var view = view
    private val gd_background by lazy { GradientDrawable() }
    private val gd_background_press by lazy { GradientDrawable() }
    private val radiusArr by lazy { mutableListOf<Float>() }

    var rvbackgroundColor: Int = Color.TRANSPARENT
        set(value) {
            field = value
            setBgSelector()
        }

    var rvbackgroundStartColor = Color.TRANSPARENT
        set(value) {
            field = value
            setBgSelector()
        }

    var rvbackgroundEndColor = Color.TRANSPARENT
        set(value) {
            field = value
            setBgSelector()
        }
    var rvbackgroundPressColor = Int.MAX_VALUE
        set(value) {
            field = value
            setBgSelector()
        }
    var rvbackgroundPressStartColor = Int.MAX_VALUE
        set(value) {
            field = value
            setBgSelector()
        }

    var rvbackgroundPressEndColor = Int.MAX_VALUE
        set(value) {
            field = value
            setBgSelector()
        }
    var rvcornerRadius = 0
        set(value) {
            field = value
            setBgSelector()
        }
    var rvstrokeWidth = 0
        set(value) {
            field = value
            setBgSelector()
        }
    var rvstrokeColor = Color.TRANSPARENT
        set(value) {
            field = value
            setBgSelector()
        }
    var rvstrokePressColor = Int.MAX_VALUE
        set(value) {
            field = value
            setBgSelector()
        }
    var rvstrokeDashGap = 0
        set(value) {
            field = value
            setBgSelector()
        }
    var rvstrokeDashWidth = 0
        set(value) {
            field = value
            setBgSelector()
        }
    var rvtextPressColor = Int.MAX_VALUE
        set(value) {
            field = value
            setBgSelector()
        }
    var rvcornerRadius_TL = 0
        set(value) {
            field = value
            setBgSelector()
        }
    var rvcornerRadius_TR = 0
        set(value) {
            field = value
            setBgSelector()
        }
    var rvcornerRadius_BL = 0
        set(value) {
            field = value
            setBgSelector()
        }
    var rvcornerRadius_BR = 0
        set(value) {
            field = value
            setBgSelector()
        }
    var rvisRadiusHalfHeight = false
        set(value) {
            field = value
            setBgSelector()
        }
    var rvisWidthHeightEqual = false
        set(value) {
            field = value
            setBgSelector()
        }
    var rvisRippleEnable = true
        set(value) {
            field = value
            setBgSelector()
        }

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.RoundAppCompatTextView)
        rvbackgroundColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_backgroundColor, Color.TRANSPARENT)
        rvbackgroundStartColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_backgroundStartColor, Color.TRANSPARENT)
        rvbackgroundEndColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_backgroundEndColor, Color.TRANSPARENT)
        rvbackgroundPressColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_backgroundPressColor, Int.MAX_VALUE)
        rvbackgroundPressStartColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_backgroundPressStartColor, Int.MAX_VALUE)
        rvbackgroundPressEndColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_backgroundPressEndColor, Int.MAX_VALUE)
        rvcornerRadius = ta.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rv_cornerRadius, 0)
        rvstrokeWidth = ta.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rv_strokeWidth, 0)
        rvstrokeColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_strokeColor, Color.TRANSPARENT)
        rvstrokePressColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_strokePressColor, Int.MAX_VALUE)
        rvstrokeDashGap = ta.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rv_strokeDashGap, 0)
        rvstrokeDashWidth = ta.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rv_strokeDashWidth, 0)
        rvtextPressColor = ta.getColor(R.styleable.RoundAppCompatTextView_rv_textPressColor, Int.MAX_VALUE)
        rvisRadiusHalfHeight = ta.getBoolean(R.styleable.RoundAppCompatTextView_rv_isRadiusHalfHeight, false)
        rvisWidthHeightEqual = ta.getBoolean(R.styleable.RoundAppCompatTextView_rv_isWidthHeightEqual, false)
        rvcornerRadius_TL = ta.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rv_cornerRadius_TL, 0)
        rvcornerRadius_TR = ta.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rv_cornerRadius_TR, 0)
        rvcornerRadius_BL = ta.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rv_cornerRadius_BL, 0)
        rvcornerRadius_BR = ta.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rv_cornerRadius_BR, 0)
        rvisRippleEnable = ta.getBoolean(R.styleable.RoundAppCompatTextView_rv_isRippleEnable, true)
        ta.recycle()
    }

    private fun setDrawable(gd: GradientDrawable, backgroundStartColor: Int, backgroundEndColor: Int, strokeColor: Int) {
        gd.orientation = GradientDrawable.Orientation.LEFT_RIGHT
        gd.colors = intArrayOf(backgroundStartColor, backgroundEndColor)
        if (rvcornerRadius_TL > 0 || rvcornerRadius_TR > 0 || rvcornerRadius_BL > 0 || rvcornerRadius_BR > 0) {
            radiusArr.clear()
            radiusArr.add(rvcornerRadius_TL.toFloat())
            radiusArr.add(rvcornerRadius_TL.toFloat())
            radiusArr.add(rvcornerRadius_TR.toFloat())
            radiusArr.add(rvcornerRadius_TR.toFloat())
            radiusArr.add(rvcornerRadius_BR.toFloat())
            radiusArr.add(rvcornerRadius_BR.toFloat())
            radiusArr.add(rvcornerRadius_BL.toFloat())
            radiusArr.add(rvcornerRadius_BL.toFloat())
            gd.cornerRadii = radiusArr.toFloatArray()
        } else {
            gd.cornerRadius = rvcornerRadius.toFloat()
        }

        gd.setStroke(rvstrokeWidth, strokeColor, rvstrokeDashWidth.toFloat(), rvstrokeDashGap.toFloat())
    }

    private fun setDrawable(gd: GradientDrawable, color: Int, strokeColor: Int) {
        gd.orientation = GradientDrawable.Orientation.LEFT_RIGHT
        gd.setColor(color)
        if (rvcornerRadius_TL > 0 || rvcornerRadius_TR > 0 || rvcornerRadius_BL > 0 || rvcornerRadius_BR > 0) {
            radiusArr.clear()
            radiusArr.add(rvcornerRadius_TL.toFloat())
            radiusArr.add(rvcornerRadius_TL.toFloat())
            radiusArr.add(rvcornerRadius_TR.toFloat())
            radiusArr.add(rvcornerRadius_TR.toFloat())
            radiusArr.add(rvcornerRadius_BR.toFloat())
            radiusArr.add(rvcornerRadius_BR.toFloat())
            radiusArr.add(rvcornerRadius_BL.toFloat())
            radiusArr.add(rvcornerRadius_BL.toFloat())
            LogUtils.e(radiusArr.toString())
            gd.cornerRadii = radiusArr.toFloatArray()
        } else {
            gd.cornerRadius = rvcornerRadius.toFloat()
        }
        gd.setStroke(rvstrokeWidth, strokeColor, rvstrokeDashWidth.toFloat(), rvstrokeDashGap.toFloat())
    }

    fun setBgSelector() {
        var bg = StateListDrawable()

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && rvisRippleEnable) {
            view.background = if (rvbackgroundStartColor != 0 && rvbackgroundEndColor != 0) {
                setDrawable(gd_background, rvbackgroundStartColor, rvbackgroundEndColor, rvstrokeColor)
                bg.addState(intArrayOf(-android.R.attr.state_pressed), gd_background)
                if (rvbackgroundPressStartColor != Int.MAX_VALUE && rvbackgroundPressEndColor != Int.MAX_VALUE || rvstrokePressColor != Int.MAX_VALUE) {
                    setDrawable(gd_background_press, rvbackgroundPressStartColor, rvbackgroundPressEndColor, rvstrokePressColor)
                    bg.addState(intArrayOf(android.R.attr.state_pressed), gd_background_press)
                }
                bg
            } else {
                setDrawable(gd_background, rvbackgroundColor, rvstrokeColor)
                val rippleDrawable = RippleDrawable(getPressedColorSelector(rvbackgroundColor, rvbackgroundPressColor), gd_background, null)
                rippleDrawable
            }
        } else {
            if (rvbackgroundStartColor != 0 && rvbackgroundEndColor != 0) {
                setDrawable(gd_background, rvbackgroundStartColor, rvbackgroundEndColor, rvstrokeColor)
                bg.addState(intArrayOf(-android.R.attr.state_pressed), gd_background)
                if (rvbackgroundPressStartColor != Int.MAX_VALUE && rvbackgroundPressEndColor != Int.MAX_VALUE || rvstrokePressColor != Int.MAX_VALUE) {
                    setDrawable(gd_background_press, rvbackgroundPressStartColor, rvbackgroundPressEndColor, rvstrokePressColor)
                    bg.addState(intArrayOf(android.R.attr.state_pressed), gd_background_press)
                }
            } else {
                setDrawable(gd_background, rvbackgroundColor, rvstrokeColor)
                bg.addState(intArrayOf(-android.R.attr.state_pressed), gd_background)
                if (rvbackgroundPressColor != Int.MAX_VALUE || rvstrokePressColor != Int.MAX_VALUE) {
                    setDrawable(
                        gd_background_press,
                        if (rvbackgroundPressColor == Int.MAX_VALUE) rvbackgroundColor else rvbackgroundPressColor,
                        if (rvstrokePressColor == Int.MAX_VALUE) rvstrokeColor else rvstrokePressColor
                    )
                    bg.addState(intArrayOf(android.R.attr.state_pressed), gd_background_press)
                }
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { //16
                view.background = bg
            } else {
                view.setBackgroundDrawable(bg)
            }
        }

        if (view is AppCompatTextView) {
            if (rvtextPressColor != Int.MAX_VALUE) {
                (view as AppCompatTextView).setTextColor(ColorStateList.valueOf(rvtextPressColor))
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private fun getPressedColorSelector(normalColor: Int, pressedColor: Int): ColorStateList {
        return ColorStateList(
            arrayOf(
                intArrayOf(android.R.attr.state_pressed),
                intArrayOf(android.R.attr.state_focused),
                intArrayOf(android.R.attr.state_activated),
                intArrayOf()
            ), intArrayOf(pressedColor, pressedColor, pressedColor, normalColor)
        )
    }

}